﻿namespace FasadPattern.FileWorker
{
    using System.IO;

    public class FileBuilder
    {
        #region Public Logic

        /// <summary>
        /// Creates empty file
        /// </summary>
        /// <param name="path">Path to file</param>
        public void BuildEmptyFile(string path)
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            m_filePath = path;

            using (FileStream fs = File.Create(path))
            {
                fs.Flush();
            }
        }

        /// <summary>
        /// Writes message into file
        /// </summary>
        /// <param name="mB">Message Builder</param>
        public void WriteMessage(MessageBuilder mB)
        {
            if (File.Exists(m_filePath))
            {
                using (StreamWriter writter = new StreamWriter(m_filePath))
                {
                    writter.WriteLine(mB.BuildMessage());
                }
            }
        }

        /// <summary>
        /// Reads Message from file
        /// </summary>
        /// <returns>Message from file</returns>
        public string ReadMessage()
        {
            string rezult = string.Empty;

            if (File.Exists(m_filePath))
            {
                using (StreamReader reader = new StreamReader(m_filePath))
                {
                    rezult = reader.ReadLine();
                }
            }
            return rezult;
        }

        #endregion

        #region Private Fields

        private string m_filePath = string.Empty;

        #endregion
    }
}
